// pages/ScenicArea/ScenicAreaChooseTickets/index.js
var common = require("../../../public.js");
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scenicTicketId: '',
    currentMonth: '', // 当前月份
    ticketPriceList: 1,
    dateDisplayList: [], // 显示的三个时间 默认为获取到时间的前三项
    currenTicketDate: '', // 当前选中的票价
    timeList: [], // 入园时间
    enterInvoiceExplain: '', // 备注说明
    currentTime: '', //当前选中的入园时间
    ticketCount: 1, //票数量
    totalMoney: 0, // 总价
    showCalendar: false,
    formatter: '',
    isLoadingBusy: false
  },

  onLoad: function (options) {
    that = this
    const title = options.storeName + '-' + options.ticketName
    wx.setNavigationBarTitle({ title })
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.setData({
          isLoadingBusy: true,
          userInfo: data.user,
          token: data.token,
          openid: data.openid,
          currentMonth: common.dateFormat('yyyy-MM', new Date()),
          scenicTicketId: options.ticketId,
          storeId: options.storeId,
          ticketName: options.ticketName,
          storeName: options.storeName
        }, _ => {
          that.initData()
        })
      } else {
        common.showLoginPage()
      }
    })
  },
  onConfirm(e) {
    if (this.data.showCalendar) {
      let current = common.dateFormat('yyyy-MM', e.detail)
      if (this.data.currentMonth != current) {
        this.setData({
          currentMonth: current
        })
      }
    }
  },
  initData() {
    let that = this
    let tempJson = {
      scenicTicketId: that.data.scenicTicketId,
      searchMonth: that.data.currentMonth
    }
    common.post('/scenicTicket/getScenicTicketDatePriceList', tempJson,
      function (res) {
        if (res.status === 200) {
          let dateDisplayList = []
          if (res.data.ticketPriceList.length >= 3) {
            dateDisplayList = [res.data.ticketPriceList[0], res.data.ticketPriceList[1], res.data.ticketPriceList[2]]
          } else {
            dateDisplayList = res.data.ticketPriceList
          }
          that.setData({
            ticketPriceList: res.data.ticketPriceList,
            timeList: res.data.timeList,
            enterInvoiceExplain: res.data.enterInvoiceExplain ? res.data.enterInvoiceExplain : '',
            dateDisplayList
          }, _ => {
            that.setData({
              formatter: function (day) {
                const month = day.date.getMonth() + 1;
                const date = day.date.getDate();
                let list = res.data.ticketPriceList
                let monthList = list.map(item => {
                  return new Date(item.saleDate).getMonth() + 1
                })
                monthList = Array.from(new Set(monthList))
                if (monthList.includes(month)) {
                  let arr = list.filter(i => {
                    return new Date(i.saleDate).getDate() == date
                  })
                  if (arr[0]) {
                    if (arr[0].todayPrice < 0) {
                      day.type = 'disabled'
                      day.bottomInfo = '闭馆'
                    } else {
                      day.bottomInfo = '￥' + arr[0].todayPrice
                    }
                  } else {
                    day.type = 'disabled'
                  }

                  // for (let i = 0; i < list.length; i++) {
                  //   const item = list[i];
                  //   let saleMonth = new Date(item.saleDate).getMonth() + 1
                  //   let saleDate = new Date(item.saleDate).getDate();
                  //   if (month === saleMonth && date === saleDate) {
                  //     day.bottomInfo = "￥" + item.todayPrice;
                  //   }
                  // }
                } else {
                  day.type = 'disabled'
                }
                return day
              },
            })
          })
        }
      },
      function () {
        that.setData({
          isLoadingBusy: false
        })
      })
  },
  submitOrder() { //提交订单
    if (common.isEmptyString(this.data.currenTicketDate)) {
      common.showToast('请选择日期')
      return
    }
    if (common.isEmptyString(this.data.currentTime)) {
      common.showToast('请选择入园时间')
      return
    }
    if (this.data.currenTicketDate.item.saleDate < new Date(new Date(new Date().toLocaleDateString()).getTime())) {
      common.showToast('请选择正确日期')
      return
    }

    let enterDate = common.dateFormat('yyyy-MM-dd', this.data.currenTicketDate.item.saleDate)
    let enterWeekday = common.getWeekDate(this.data.currenTicketDate.item.saleDate)
    console.log(enterWeekday)
    wx.navigateTo({
      url: '/pages/ScenicArea/ScenicAreaCommitPage/index?storeId=' + this.data.storeId +
        '&ticketId=' + this.data.scenicTicketId +
        '&ticketName=' + this.data.ticketName +
        '&storeName=' + this.data.storeName +
        '&ticketPrice=' + this.data.currenTicketDate.item.todayPrice +
        '&ticketNum=' + this.data.ticketCount +
        '&ticketTimeId=' + this.data.currentTime.item.id +
        '&enterDate=' + enterDate +
        '&enterWeekday=' + enterWeekday +
        '&startTime=' + this.data.currentTime.item.startTime +
        '&endTime=' + this.data.currentTime.item.endTime
    })
  },
  selectDate(e) {
    let currentObj = e.currentTarget.dataset
    if (currentObj.item.todayPrice < 0) {
      common.showToast('当日闭馆')
      return
    }
    this.setData({
      currenTicketDate: currentObj,
      totalMoney: currentObj.item.todayPrice * 100 * this.data.ticketCount
    })
  },
  selectTime(e) { //点击选择入园时间
    let currentObj = e.currentTarget.dataset
    console.log(currentObj.item)
    if (currentObj.item.stock == currentObj.item.saledNums) { // 库存==销售量 bu
      return false
    } else {
      this.setData({
        currentTime: currentObj
      })
    }
  },
  selectCalendar(e) {
    let selectedDate = e.detail.getDate()
    let selectedMonth = e.detail.getMonth()
    let serveTime = this.data.ticketPriceList
    let arr = serveTime.filter(i => {
      return new Date(i.saleDate).getDate() == selectedDate && new Date(i.saleDate).getMonth() == selectedMonth
    })
    this.setData({
      currenTicketDate: {
        index: 2,
        item: arr[0]
      },
      dateDisplayList: [this.data.dateDisplayList[0], this.data.dateDisplayList[1], arr[0]],
      totalMoney: arr[0].todayPrice * 100 * this.data.ticketCount
    }, _ => {
      this.setData({
        showCalendar: false
      })
    })
  },
  openCalendar() {
    this.setData({
      showCalendar: true
    })
  },
  onCloseCalendar() {
    this.setData({
      showCalendar: false
    })
  },
  onCountChange(e) {
    if (this.data.currenTicketDate) {
      const num = e.detail
      this.setData({
        ticketCount: num,
        totalMoney: num * this.data.currenTicketDate.item.todayPrice * 100 // 单位分
      })
    } else {
      this.setData({
        ticketCount: 1
      })
      common.showToast('请先选择日期')
    }
  }
})